package cn.kmsoft.jssc.service.processor;

import cn.kmsoft.jssc.config.AnalyseConfig;
import cn.kmsoft.jssc.entity.analyse.*;
import cn.kmsoft.jssc.service.AnalyseTaskProcessor;
import cn.kmsoft.wololo.object.query.GeoJsonQuery;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.util.*;


/**
 * 三区三线的生态保护红线-空间套合叠加分析
 * 
 * @author wangq
 *
 */
public class StbhhxAnalyseTaskProcessor extends AnalyseTaskProcessor {
	private static final Logger logger = LoggerFactory.getLogger(StbhhxAnalyseTaskProcessor.class);
	//基本农田
	private final String analysePoint="stbhhx";
	private AnalyseTaskParam analyseTaskParam;
	private AnalyseConfig analyseConfig;

	public StbhhxAnalyseTaskProcessor(AnalyseTaskParam param, AnalyseConfig analyseConfig) {
		this.analyseTaskParam=param;
		this.analyseConfig=analyseConfig; 
	}
	
	public AnalyseTaskParam getAnalyseTaskParam() {
		return analyseTaskParam;
	}
	public void setAnalyseTaskParam(AnalyseTaskParam analyseTaskParam) {
		this.analyseTaskParam = analyseTaskParam;
	}

	@Override
	public TaskResult call() throws Exception {
		// TODO Auto-generated method stub
		String threadId = Thread.currentThread().getName();
		
		String tableName = this.analyseTaskParam.getTableName();
		String analyseType = this.analyseTaskParam.getAnalystType();
		String dbKey = this.analyseTaskParam.getDbKey();
		String feaTypePoint = analyseTaskParam.getFeaTypePoint();

		AnalyseDb analyseDb = this.analyseConfig.getAnalyseDbByDbkey(dbKey);
		if(analyseDb==null){
			System.out.println();
			String errorMsg = String.format("--->TaskProcessor stop, dbKey: %s, 为null",dbKey);
			System.out.println(errorMsg);
			logger.warn(errorMsg);

			return null;
		}
		String dbType = analyseDb.getDbType();
		String dbInstance = analyseDb.getDbInstance();
		String tablespace = analyseDb.getTablespace();
		String serverId = analyseDb.getServerId();//分析服务标识

		String tableId = dbInstance + "/" + tablespace + "." + tableName;
		//注意，根据数据库类型进行分类处置。
		//若为oracle的sde，则需要添加表空间；
		if("postgresql".equalsIgnoreCase(dbType)){
			//若为postgresql的postgis库，则去掉表空间或者为postgis_33_sample.sde.dltb_2018_500155_d36
			tableId = dbInstance + "/" + dbInstance+"."+ tablespace + "." + tableName;
		}
		String operatorMethod = "intersectpy";
		if(analyseType.equalsIgnoreCase("intersect") && feaTypePoint.equalsIgnoreCase("py")){
			operatorMethod = "intersectpy";
		}else if(analyseType.equalsIgnoreCase("intersect") && feaTypePoint.equalsIgnoreCase("py2")){
			operatorMethod = "intersectpy2";
		}else if(analyseType.equalsIgnoreCase("spatialquery") && feaTypePoint.equalsIgnoreCase("py")){
			operatorMethod = "spatialQuery";
		} else if(analyseType.equalsIgnoreCase("spatialquery") && feaTypePoint.equalsIgnoreCase("py2")){
			operatorMethod = "spatialQuery2";
		} else if(analyseType.equalsIgnoreCase("difference") && feaTypePoint.equalsIgnoreCase("py")){
			operatorMethod = "difference";
		}

		AnalyseServer analyseServer = this.analyseConfig.getAnalyseServerByServerId(serverId);
		String serverUrl = analyseServer.getServerUrl();

		String targetUrl = serverUrl + "/" + operatorMethod + "/" + tableId;
		//String targetUrl = this.analyseConfig.getServerUrl() + "/" + operatorMethod + "/" + tableId;
		//关键参数，叠加分析的目标URL
		//String targetUrl = this.analyseConfig.getServerUrl() + "/" + analystType + "/" + tableId;
		//String targetUrl = this.analyseConfig.getAnalystServiceUriMap().get(this.analysePoint);
		
		//任务参数
		AnalyseTaskParam params = this.analyseTaskParam;
		params.setTargetUrl(targetUrl);
		
		TaskResult taskResult = this.startAnalyse(operatorMethod,threadId, params);

		return taskResult;
	}

	/**
	 * start分析
	 *
	 * TaskParam params
	 * Return
	 */
	@Override
	public TaskResult startAnalyse(String operatorMethod,String threadId, AnalyseTaskParam params) {
		TaskResult result = new TaskResult();

		String tableName= params.getTableName();
		String targetUrl = params.getTargetUrl();
		String jobId= params.getJobId();
		String taskId = params.getTaskId();
		String dkbh = params.getDkbh();
		GeoJsonQuery query = (GeoJsonQuery) params.getQuery();

		result.setThreadId(threadId);
		result.setJobId(jobId);
		result.setAnalystPoint(this.analysePoint);
		result.setProcessorName(params.getProcessName());
		result.setTaskId(taskId);
		result.setDkbh(dkbh);
		result.setTableName(tableName);

		System.out.println(String.format("--->doAnalyse,threadId: %s, jobId: %s, dkbh: %s, targetUrl: %s",threadId, jobId, dkbh, targetUrl));
		//核心代码
		//叠加分析
		List<StbhhxDetailResult> stbhhxDetailList = this.doAnalyse(jobId, targetUrl, query);
		if(stbhhxDetailList.size()>0){
			StbhhxDetailResult detailResult = stbhhxDetailList.get(0);
			double sgeoarea = detailResult.getSgeoarea();
			result.setDkmj(sgeoarea);
		}

		System.out.println(String.format("--->threadId: %s, jobId: %s, analysePoint: %s,生态保护红线压占分析的详细数量： %d", threadId, jobId, this.analysePoint, stbhhxDetailList.size()));
		Map<String, Statis03Result> yjjbnt03Summary = this.summaryStbhhxStatis03Result(stbhhxDetailList);
		JbntZySummary01Result yjjbnt01Summary = this.summaryStbhhxStatis01Result(stbhhxDetailList);

		result.setDetailsListResult(stbhhxDetailList);
		result.setSummary03Result(yjjbnt03Summary);
		result.setSummary03DBEntResult(null);
		result.setSummary02Result(null);
		result.setSummary01Result(yjjbnt01Summary);

		return result;
	}
	
	
	/**
	 * 生态保护红线压占分析
	 * 
	 * @param query
	 * @return
	 */
	public List<StbhhxDetailResult> doAnalyse(String jobId, String targetUrl, GeoJsonQuery query) {

		List<StbhhxDetailResult> stbhhxDetailList = new ArrayList<StbhhxDetailResult>();
		
		String queryJsonStr = JSONObject.toJSONString(query);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		
		HttpEntity<String> entity = new HttpEntity<>(queryJsonStr, headers);
		
		//2、转发空间分析的请求
		ResponseEntity<JSONObject> exchange = this.exchangeSpatialAnalyst(jobId, targetUrl, entity);
		JSONObject stbhhxObject= exchange.getBody();
		if(stbhhxObject==null || stbhhxObject.getJSONArray("result")==null) {
			return stbhhxDetailList;
		}
		
		JSONArray feasJSONArray=stbhhxObject.getJSONArray("result").getJSONObject(0).getJSONArray("features");
		//获取图形和属性
		List<Map<String,String>> allFeaProperties = this.parseFeaGeometryAndProperties(feasJSONArray);
		int feaCount = allFeaProperties.size();		
		for(int i=0; i<feaCount;i++ ) {
			Map<String,String> feaProps = allFeaProperties.get(i);

			//将要素属性的key全部转为小写，否则oracle和postgresql数据库返回的结果会有差异
			Map<String,String> feaPropsKeyLowercCase = new HashMap<>();
			feaProps.forEach((key,value) -> feaPropsKeyLowercCase.put(key.toLowerCase(), value));

			//1-目标图层的字段值
			String bsm =  feaPropsKeyLowercCase.get("bsm").toString();//BSM
			String hxbm = feaPropsKeyLowercCase.get("hxbm").toString();//红线代码HXBM
			String lxdm = feaPropsKeyLowercCase.get("lxbm").toString();//类型编码LXBM
			String hxlx = feaPropsKeyLowercCase.get("hxlx").toString();//红线类型HXLX

			//2-追加的3个几何面积
			double area = Double.parseDouble(feaPropsKeyLowercCase.get("area").toString());//叠加后重叠范围的几何面积
			double fgeoarea = Double.parseDouble(feaPropsKeyLowercCase.get("fgeoarea").toString());//目标图层（sde库）原图斑几何面积
			double sgeoarea = Double.parseDouble(feaPropsKeyLowercCase.get("sgeoarea").toString());//上传图斑的几何面积
			//3-叠加相交结果的几何图形
			String geomText = feaPropsKeyLowercCase.get("geom").toString();

			StbhhxDetailResult stbhhxDetail = new StbhhxDetailResult();
			stbhhxDetail.setFeaProps(feaProps);

			stbhhxDetail.setBsm(bsm);
			stbhhxDetail.setHxbm(hxbm);
			stbhhxDetail.setLxbm(lxdm);
			stbhhxDetail.setHxlx(hxlx);
			stbhhxDetail.setArea( Math.abs(area) );	//套合相交面积
			stbhhxDetail.setFgeoarea(Math.abs(fgeoarea));//目标图层（sde库）原图斑几何面积
			stbhhxDetail.setSgeoarea(Math.abs(sgeoarea));//上传图斑几何面积
			stbhhxDetail.setGeomText(geomText);

			stbhhxDetailList.add(stbhhxDetail);
		}
		
	    return stbhhxDetailList;
	}

	/**
	 * 生态保护红线压占情况汇总
	 *
	 * @param stbhhxDetailList
	 * @return
	 */
	public JbntZySummary01Result summaryStbhhxStatis01Result(List<StbhhxDetailResult> stbhhxDetailList) {
		double gjmj=0.0;//共计

		Iterator<StbhhxDetailResult> iter= stbhhxDetailList.iterator();
		while(iter.hasNext()) {
			StbhhxDetailResult jbntZyDetail = iter.next();

			String lxbm = jbntZyDetail.getLxbm();
			double djArea = jbntZyDetail.getArea();//相交叠加面积

			gjmj += djArea;
		}

		JbntZySummary01Result jbntZySummary = new JbntZySummary01Result();
		jbntZySummary.setGjmj(gjmj);

		return jbntZySummary;
	}

	/**
	 * 对生态保护红线按类型代码进行面积汇总至3级类
	 *
	 * @param stbhhxDetailList
	 * @return
	 */
	public Map<String,Statis03Result> summaryStbhhxStatis03Result(List<StbhhxDetailResult> stbhhxDetailList) {
		Map<String, Statis03Result> stbhhxStatisResults = new HashMap<String, Statis03Result>();
		// 汇总为三级分类统计
		Iterator<StbhhxDetailResult> detailIter = stbhhxDetailList.iterator();
		while (detailIter.hasNext()) {
			StbhhxDetailResult jbnt = detailIter.next();
			String lxbm = jbnt.getLxbm();//类型编码
			String hxlx = jbnt.getHxlx();//红线类型
			double area = jbnt.getArea();

			//生态保护红线的类型代码
			if (stbhhxStatisResults.containsKey(lxbm)) {
				double areaOfSum = stbhhxStatisResults.get(lxbm).getAnalyseArea();
				
				stbhhxStatisResults.get(lxbm).setAnalyseArea(areaOfSum + area);
			} else {
				Statis03Result statis03Result = new Statis03Result();
				statis03Result.setDm(lxbm);
				statis03Result.setMc(hxlx);
				statis03Result.setAnalyseArea(area);

				stbhhxStatisResults.put(lxbm, statis03Result);
			}
		}

		return stbhhxStatisResults;
	}
	
}
